/*
https://leetcode.cn/problems/capacity-to-ship-packages-within-d-days/
*/
class Solution {
public:
    int shipWithinDays(vector<int>& weights, int days) {
    int l =  *max_element(weights.begin(), weights.end()), r = 500*50000;
        while(l<r){
            int mid=(l+r)/2;
            int sum=0,count=1;
            for(int i:weights){
                if(sum+i>mid){
                    ++count;
                    sum=0;
                } 
                sum+=i;
            }
            if(count<=days){
                r=mid;
            }
            else l=mid+1;
        }
        return l;
    }
};